package cn.sx.dao;

/*
 *@program:sx_emall_ssm
 *@author: sx
 *@desc: 商品信息接口
 *@Time: 2020/9/20  12:06
 */

import cn.sx.entity.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface GoodsDao {

    /*获取总数*/
    @Select("select count(*) from goods")
    int selectCount();

    /*新品上市*/
    @Select("select * from goods  ORDER BY id desc limit #{begin},#{size} ")
    public List<Goods> selectList(@Param("begin") int begin, @Param("size") int size);


    @Select("select count(*) from goods where id in(${ids})")
    int selectCountByIds(@Param("ids") String ids);

    /*商品分类，获取列名根据typeid*/
    @Select("select count(*) from goods where type_id = #{typeId}")
    int selectCountByTypeId(@Param("typeId") int typeId);

    @Select("select * from goods where id in (${ids}) order by id desc limit #{begin} ,#{size}")
    List<Goods> selectListByIds(@Param("ids") String ids, @Param("begin") int begin, @Param("size") int size);

    /*根据id显示商品分类*/
    @Select("select * from goods where type_id=#{typeId} order by id desc limit #{begin},#{size}")
    List<Goods> selectListByTypeId(@Param("typeId") int typeId, @Param("begin") int begin, @Param("size") int size);

    /*今日推荐*/
    @Select("select * from goods g join tops t on g.id =t.good_id where t.type=#{type} order by t.id desc limit #{begin},#{size}")
    List<Goods> selectListByTopType(@Param("type") byte type, @Param("begin") int begin, @Param("size") int size);

    @Select("select * from goods where name like concat ('%',#{name},'%')order by id desc limit #{begin},#{size}")
    List<Goods> selectListByName(@Param("name") String name, @Param("begin") int begin, @Param("size") int size);

    @Select("select * from goods where id=#{id}")
    Goods select(int id);

    @Select("SELECT g.id,g.cover,g.name,g.intro,g.spec,g.price,t.name,g.stock,g.sales FROM goods g JOIN `types` t ON g.type_id=t.id ORDER BY g.id desc limit #{begin},#{size}")
    List<Goods>selectAllGoods(@Param("begin")int begin,@Param("size")int size);

    @Select("select count(*) from goods g join `tops` t on g.id=t.good_id where t.type=#{type}")
    int selectCountByTopType(@Param("type") byte type);

    /*热销排行*/
    @Select("select * from goods  ORDER BY sales desc limit #{begin},#{size}")
    List<Goods> getListOrderSales(@Param("begin") int begin, @Param("size") int size);

    /*h后台修改商品信息*/
    @Update("UPDATE goods SET cover=#{cover},`name`=#{name},intro=#{intro},spec=#{spec}," +
            "price=#{price},stock=#{stock},type_id=#{typeId},content=#{content} WHERE id=#{id}")
    boolean update(Goods goods);

    @Update("update goods set stock=stock-#{stock} where id=#{id}")
    boolean updateStock(@Param("id") int id, @Param("stock") int stock);

    @Update("update goods set sales =sales+#{sales} where id=#{id}")
    boolean updateSales(@Param("id") int id,@Param("sales") int sales);

    @Update("delete from goods where id =#{id}")
    boolean delete(int id);

    @Insert("insert into goods (cover,name,intro,spec,content,price,stock,type_id)" +
            "values(#{cover},#{name},#{intro},#{spec},#{content},#{price},#{stock},#{typeId})")
    @SelectKey(keyProperty = "id",statement = "SELECT LAST_INSERT_ID()",before = false,resultType = Integer.class)
    boolean insert(Goods good);
}
